//
//  CircleViewController.m
//  悠狸
//
//  Created by dllo on 15/9/1.
//  Copyright (c) 2015年 Dung. All rights reserved.
//

#import "CircleViewController.h"
#import "CircleCell.h"
#import "CateModel.h"

#import "Asyn.h"
#import "Tools.h"
#import "URL.h"
#import "Webview.h"


@interface CircleViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)UITableView *table;
@property (nonatomic, retain)NSMutableArray *array;
@end

@implementation CircleViewController
- (void)dealloc
{
    [_array release];
    [_cate release];
    [_table release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = self.cate.name;
    [self create];
    [self parser];
}

- (void)parser
{
    

    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?limit=20&offset=0&gender=1&generation=2", self.cate.Id];

    [Asyn  startParser:str partemer:nil file:@"data12" block:^(NSData *data) {
        
        //存储
        [Tools toolsOfSaveLibraryPathName:@"data12" data:data];
        // 解析
        NSDictionary *dic = [Tools toolsOfJsonDataType:dicType fileName:@"data12"];
   
        
        NSDictionary *dic1 =[dic objectForKey:@"data"];
        
        NSArray *arr = [dic1 objectForKey:@"items"];
        
#pragma 清楚数组
        [_array removeAllObjects];
        
        for (NSDictionary * dic2 in arr) {
            
            CateModel *model = [[CateModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dic2];
            
            [_array addObject:model];
            
        }
        [_table reloadData];
        
    }];
    
}

- (void)create
{
    self.table = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) style:UITableViewStylePlain] autorelease];
    _table.delegate = self;
    _table.dataSource = self;
    _table.rowHeight = 160*OffHeight;
    _table.separatorColor = [UIColor orangeColor];
    
    [self.view addSubview:_table];
    
    [_table registerClass:[CircleCell class] forCellReuseIdentifier:@"j"];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CircleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"j" forIndexPath:indexPath];
    CateModel *model = [_array objectAtIndex:indexPath.row];
    
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    [cell sendCate:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    Webview *web = [[[Webview alloc] init] autorelease];

    CateModel *cate = [_array objectAtIndex:indexPath.row];
    web.cate = cate;
    
    [self.navigationController pushViewController:web animated:YES];
    
}



@end
